#! /bin/sh
#
# Unix sh variant of h5lsserie (h5lsserie.in)
# If editing this file, also edit the file h5lsserie.bat.in
#
# Copyright (C) 2009 Markus Friedrich
#
# This library is free software; you can redistribute it and/or 
# modify it under the terms of the GNU Lesser General Public 
# License as published by the Free Software Foundation; either 
# version 2.1 of the License, or (at your option) any later version. 
#  
# This library is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
# Lesser General Public License for more details. 
#  
# You should have received a copy of the GNU Lesser General Public 
# License along with this library; if not, write to the Free Software 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
#
# Contact:
#   mafriedrich@users.berlios.de
#

H=0 # option -h, --help: help
D=0 # option -d: show description
L=0 # optoin -l: show Column/Member Label
F=0 # option -f: follow external links
while test $# -ne 0; do
  case $1 in
    -d)        D=1;;
    -h|--help) H=1;;
    -l)        L=1;;
    -f)        F=1;;
    -*)        echo "Invalid option: $1; see output of h5lsserie"
               exit 1;;
    *)         FILE="$1";;
  esac
  shift
done

if [ $H -eq 1 ]; then
  echo "h5lsserie"
  echo ""
  echo "Lists the groups and datasets in a hdf5 file."
  echo ""
  echo "Copyright (C) 2009 Markus Friedrich <mafriedrich@users.berlios.de>"
  echo "This is free software; see the source for copying conditions. There is NO"
  echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
  echo ""
  echo "Licensed under the GNU Lesser General Public License (LGPL)"
  echo ""
  echo "Usage:"
  echo "  h5lsserie [-d] [-l] <file.h5>"
  echo "    -h, --help: Show this help"
  echo "    -d:         Show 'Description' attribute"
  echo "    -l:         Show 'Column/Member Label'"
  echo "    -f:         Follow external links"
  exit 0
fi

prefix="@prefix@"
exec_prefix="@exec_prefix@"
datadir="@datadir@"
h5dump="@h5dump@"
xsltproc="@xsltproc@"

# get environment variables (for none default intallation)
test $HDF5SERIEDATADIR && datadir="$HDF5SERIEDATADIR"
test $HDF5SERIEH5DUMP && h5dump="$HDF5SERIEH5DUMP"
test $HDF5SERIEXSLTPROC && xsltproc="$HDF5SERIEXSLTPROC"

# change to dir
DIR="$(dirname "$FILE")"
FILE=$(basename "$FILE")
cd "$DIR"

# create XML dump (without data) of FILE and all linked files (recursive)
ALLGENERATEDFILES=""
function xmldump {
  OUT=.$1.xml
  "$h5dump" -xA $1 > $OUT
  ALLGENERATEDFILES="$ALLGENERATEDFILES $OUT"
  if [ $F -eq 1 ]; then
    for SUBFILE in $("$xsltproc" --path . "$datadir/hdf5serie/xsl/h5lsserie-listlinks.xsl" $OUT); do
      xmldump $SUBFILE
    done
  fi
}
xmldump $FILE

# call xslt
"$xsltproc" --path . --param DIR "'$DIR'" --param FILENAME "'$FILE'" --param LABEL $L --param DESCRIPTION $D --param FOLLOW $F "$datadir/hdf5serie/xsl/h5lsserie.xsl" .$FILE.xml

# delete all previous created .*.h5.xml files
for SUBFILE in $ALLGENERATEDFILES; do
  rm -f $SUBFILE
done
